package com.Test.LoginManager.client;

//import com.gargoylesoftware.htmlunit.javascript.host.Window;
import model.client.Users;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.datepicker.client.DatePicker;

public class Login extends Composite {
	private TextBox txtUsuario;
	private PasswordTextBox txtPassword;

	public Login() {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setHeight("168px");
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		initWidget(verticalPanel);
		
		Label lblAccedaASu = new Label("Acceda a su cuenta");
		lblAccedaASu.setHeight("36px");
		lblAccedaASu.setStyleName("gwt-LoginTitle");
		verticalPanel.add(lblAccedaASu);
		
		FlexTable flexTable_1 = new FlexTable();
		flexTable_1.setHeight("66px");
		verticalPanel.add(flexTable_1);
		
		Label lblUsuario = new Label("Usuario: ");
		flexTable_1.setWidget(0, 0, lblUsuario);
		lblUsuario.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		lblUsuario.setStyleName("gwt-LoginLabel");
		
		txtUsuario = new TextBox();
		flexTable_1.setWidget(0, 1, txtUsuario);
		
		Label lblPassword = new Label("Password: ");
		flexTable_1.setWidget(1, 0, lblPassword);
		lblPassword.setStyleName("gwt-LoginLabel");
		
		txtPassword = new PasswordTextBox();
		flexTable_1.setWidget(1, 1, txtPassword);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		
		CheckBox chkNoCerrarSesion = new CheckBox("No cerrar sesi\u00F3n");
		flexTable_1.setWidget(2, 0, chkNoCerrarSesion);
		chkNoCerrarSesion.setStyleName("gwt-LoginCheckBox");
		flexTable_1.getFlexCellFormatter().setColSpan(2, 0, 2);
		flexTable_1.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
		
		Button btnAcceder = new Button("New button");
		flexTable_1.setWidget(3, 0, btnAcceder);
		btnAcceder.setSize("67px", "30px");
		btnAcceder.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(txtUsuario.getText().length() == 0 || txtPassword.getText().length() == 0){
					Window.alert("Debe especificar un nombre de usuario y contrase\u00F1a");					
				}
				else{
					if(Users.isAuthorizedUser(txtUsuario.getText(), txtPassword.getText())){
						RootPanel.get().clear();
						
					}
				}
			}			
		});
		btnAcceder.setText("Acceder");
		flexTable_1.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTable_1.getFlexCellFormatter().setColSpan(3, 0, 2);
	}

}
